package com.nantai.jiafang.nantaijiafang.zhijian.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.RecyclerItemClickListener;
import com.nantai.jiafang.nantaijiafang.ViewModel.Zhijian_Net;
import com.nantai.jiafang.nantaijiafang.activity.adapter.ZhiJian_detailAdapter;
import com.nantai.jiafang.nantaijiafang.base.BaseFragment;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.MLBodyItem;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.Tip;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentZhijianDetailBinding;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查看明细界面
 */

public class ZhijianFragment_detail extends BaseFragment<FragmentZhijianDetailBinding> implements View.OnClickListener {
    private String position;
    private FragmentZhijianDetailBinding binding;
    private ArrayList<Boolean> flag;
    private ZhiJian_detailAdapter zhiJian_detailAdapter;
    public static ZhijianFragment_detail Initclass(String position){
        ZhijianFragment_detail zhijianFragment_detail=new ZhijianFragment_detail();
        zhijianFragment_detail.setPosition(position);
        return zhijianFragment_detail;
    }
    public void setPosition(String position){
        this.position=position;
    }
    @Override
    public int setlayoutid() {
        return R.layout.fragment_zhijian_detail;
    }

    @Override
    public void initUI(FragmentZhijianDetailBinding binding) {
        this.binding=binding;
        binding.setOnclickListener(this);
        //getdetaile();
        binding.recyclerDetail.addOnItemTouchListener(new RecyclerItemClickListener(getContext().getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

//                getActivity().getSupportFragmentManager().beginTransaction()
//                        .addToBackStack(null)
//                        .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
//                        .add(R.id.container2,ZhijianFragment_detail_add.newinit(zhiJian_detailAdapter.getdata(position)),"ZhijianFragment_detail_add")
//                        .commit();
            }
            @Override
            public void onLongClick(View view, int posotion) {

            }
        }));
        binding.refresh.setOnRefreshListener(()->{
            getdetaile();
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.detail_back:
                getFragmentManager().popBackStack();
                break;
            case R.id.detail_delete:
                flag=zhiJian_detailAdapter.getFlag();
                String guid3=zhiJian_detailAdapter.getGuid();
                if (guid3.length()<1)
                    return;
                deleteguid(guid3,flag);
                break;
            case R.id.detail_add:
                EventBus.getDefault().postSticky(position);
                getActivity().getSupportFragmentManager().beginTransaction()
                    .addToBackStack(null)
                    .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                    .replace(R.id.container2,new ZhijianFragment_detail_add(),"ZhijianFragment_detail_add")
                    .commit();
                break;
        }
    }
    public void getdetaile(){
        Map<String,String> maps=new HashMap<>();
        maps.put("opType","getEInspectDetail");
        maps.put("ID",position);
        Zhijian_Net.getHeardBody(getContext(),maps , new Zhijian_Net.GetSingData() {
            @Override
            public void success(Object s) {
                List<MLBodyItem> mlBodyItems=(List<MLBodyItem>)s;
                GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),1);
                binding.recyclerDetail.setLayoutManager(layoutManager);
                zhiJian_detailAdapter=new ZhiJian_detailAdapter(mlBodyItems,R.layout.fragment_zhijian_detail_item,getFragmentManager(),position);
                binding.recyclerDetail.setAdapter(zhiJian_detailAdapter);
                binding.refresh.setRefreshing(false);
            }
        });
    }
    private void deleteguid(String guid,final ArrayList<Boolean> flag){
        Map<String,String> maps=new HashMap<>();
        maps.put("opType","batchDeleteDetail");
        maps.put("Guids",guid);
        //删除表体
        Zhijian_Net.batchDeletehead(this.getContext().getApplicationContext(), maps, new Zhijian_Net.GetSingData<List<Tip>>() {
            @Override
            public void success(List<Tip> s) {
                for (int i = 0; i <flag.size() ; i++) {
                    if (flag.get(i)){
                        // flag.remove(i);
                        zhiJian_detailAdapter.removeitem(i);
                        i--;
                    }
                }
                toast(s.get(0).getTip());
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        getdetaile();
    }

}
